package com.ys.service.amazonorder.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.bo.vo.R;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.amazonorder.TbBaseinfoDwmc;
import com.ys.mapper.amazonorder.TbBaseinfoDwmcMapper;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.service.amazonorder.TbBaseinfoDwmcService;

@Service("tbBaseinfoDwmcService")
public class TbBaseinfoDwmcServiceImpl implements TbBaseinfoDwmcService {

	@Autowired
	private TbBaseinfoDwmcMapper tbBaseinfoDwmcMapper;
	@Autowired
	private DiySqlMapper diySqlMapper;
	@Override
	public R add(Map<String, Object> params) {

		TbBaseinfoDwmc entity = JSON.parseObject(JSON.toJSONString(params), TbBaseinfoDwmc.class);
		String s = "[\\u4e00-\\u9fa5]+";
		if(!entity.getNameUnitCn().matches(s)){
			return R.error("输入有误,请输入中文");
		}
		String m = "^.{1,50}$";
		if(!entity.getNameUnitEn().matches(m)){
			return R.error("输入有误,请输入英文");
		}

		if (new TbBaseinfoDwmc().setNameUnitCn(entity.getNameUnitCn()).count() > 0) {
			return R.error("该数据已存在");
		}
		if (new TbBaseinfoDwmc().setNameUnitEn(entity.getNameUnitEn()).count() > 0) {
			return R.error("该数据已存在");
		}

		tbBaseinfoDwmcMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}

	@Override
	public R edit(Map<String, Object> params) {


		TbBaseinfoDwmc entity = JSON.parseObject(JSON.toJSONString(params), TbBaseinfoDwmc.class);

		String s = "[\\u4e00-\\u9fa5]+";
		if(!entity.getNameUnitCn().matches(s)){
			return R.error("输入有误,请输入中文");
		}
		String m = "^.{1,50}$";
		if(!entity.getNameUnitEn().matches(m)){
			return R.error("输入有误,请输入英文");
		}
		
		tbBaseinfoDwmcMapper.update(entity);
		return R.ok(EditDto.closeAndReload());
	}

	/**
	 * 删除
	 * @operateCompanyId operateCompanyId
	 * @return
	 */
	@Override
	@Transactional
	public R deleteTbBaseinfoDwmc(Long id) throws Exception{
		diySqlMapper.deleteTbBaseinfoDwmc(id);
		return R.ok(EditDto.closeAndReload());
	
	}


}
